package com.danco.training.zaneuskaya.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;












import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.danco.training.zaneuskaya.api.ClaimService;
import com.danco.training.zaneuskaya.connection.HibernateSessionUtil;
import com.danco.training.zaneuskaya.connection.dao.BookDao;
import com.danco.training.zaneuskaya.connection.dao.ClaimDao;
import com.danco.training.zaneuskaya.connection.dao.impl.BookDaoImpl;
import com.danco.training.zaneuskaya.connection.dao.impl.ClaimDaoImpl;
import com.danco.training.zaneuskaya.cswworker.CSVReader;
import com.danco.training.zaneuskaya.cswworker.CSVSaver;
import com.danco.training.zaneuskaya.cswworker.Separator;
import com.danco.training.zaneuskaya.domain.Claim;

public class ClaimServiceImpl implements ClaimService {

	private static final Logger logger = Logger
			.getLogger(ClaimServiceImpl.class);
	private static ClaimServiceImpl claimStorageServiceInstance;

	ClaimDao claimDao = new ClaimDaoImpl();
	BookDao bookDao = new BookDaoImpl();

	
	public ClaimServiceImpl() {

	}

	public List<Claim> getList() {
		Session session = null;
		Transaction transaction = null;
		List<Claim> res = new ArrayList<Claim>();
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			res.addAll(claimDao.readAll(session));
			transaction.commit();
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
		}
		logger.trace("Claim list");
		return res;

	}

	public Claim getById(Integer id) {
		Claim claim = null;
		Session session = null;
		Transaction transaction = null;
		
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			claim = claimDao.read(session, id);
			transaction.commit();
			
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
		}
		return claim;

	}

	public void saveEntityListToFile(String file) {
		ArrayList<String> res = new ArrayList<String>();
		for (Claim c : getList()) {
			res.add(entityDetails(c));
		}
		try {
			CSVSaver.saveListToFile(file, res);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}

	public ArrayList<Claim> readEntityList(String file) {
		Session session = null;
		ArrayList<Claim> res = new ArrayList<Claim>();
		for (int i = 0; i < CSVReader.readFile(file).length; i++) {
			String claim[] = CSVReader.readFile(file)[i]
					.split(Separator.SEPARATOR.getS());
			try {
				session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
				Claim c = new Claim(Integer.parseInt(claim[0]), bookDao.read(
						session, Integer.parseInt(claim[1])),
						Integer.parseInt(claim[2]));
				res.add(c);
			} catch (NumberFormatException e) {
				logger.error(e.getMessage(), e);
			} catch (HibernateException e) {
				logger.error(e.getMessage(), e);
			}

		}
		return res;
	}

	public static ClaimServiceImpl getClaimStorageServiceInstance() {
		if (claimStorageServiceInstance == null) {
			claimStorageServiceInstance = new ClaimServiceImpl();
		}
		return claimStorageServiceInstance;
	}

	public String entityDetails(Claim t) {

		StringBuffer str = new StringBuffer();
		str.append(t.getId());
		str.append(Separator.SEPARATOR.getS());
		str.append(t.getBook().getId());
		str.append(Separator.SEPARATOR.getS());
		str.append(t.getClaimAmount());

		return str.toString();
	}

	@Override
	public void addEntity(Claim e) {
		Transaction transaction = null;
		Session session = null;
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			claimDao.create(session, e);
			transaction.commit();
		} catch (HibernateException e1) {
			logger.error(e1.getMessage(), e1);
			transaction.rollback();
		}

	}

	@Override
	public void updateEntity(Claim e) {
		Session session = null;
		Transaction transaction = null;
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			claimDao.update(session, e);
			transaction.commit();
		} catch (HibernateException e1) {
			logger.error(e1.getMessage(), e1);
			transaction.rollback();
		}

	}

	@Override
	public List<Claim> getClaimsSortedByAlphabet() {
		List<Claim> claims = null;
		Session session = null;
		Transaction transaction = null;
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			claims = claimDao.readClaimsByAlphabet(session);
			transaction.commit();
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
		}

		return claims;
	}

	@Override
	public List<Claim> getClaimsSortedByClaimAmount() {
		List<Claim> claims = null;
		Session session = null;
		Transaction transaction = null;
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			claims = claimDao.readClaimsByClaimAmount(session);
			transaction.commit();
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			transaction.rollback();
		}

		return claims;
	}

	@Override
	public void removeEntityById(Integer id) {
		Transaction transaction = null;
		Session session = null;
		try {
			session = HibernateSessionUtil.getSessionFactory().getCurrentSession();
			transaction = session.beginTransaction();
			Claim claim = claimDao.read(session, id);
			claimDao.delete(session, claim);

			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			logger.error(e.getMessage(), e);
		}

	}

}
